﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

using Domain.Core.Specification;

using Domain.MainModule.BankAccounts;
using Domain.MainModule.Entities;

using Infraestructure.CrossCutting.Logging;
using Infraestructure.Data.Core;
using Infraestructure.Data.Core.Extensions;
using Infraestructure.Data.MainModule.Resources;
using Infraestructure.Data.MainModule.UnitOfWork;

namespace Infraestructure.Data.MainModule.Repositories
{
    public class BankAccountRepository : Repository<BankAccount>, IBankAccountRepository
    {

        #region constructor

        
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="unitOfWork">IMainModuleUnitOfWork dependency, intented to be resolved with IoC</param>
        /// <param name="traceManager">ITraceManager context, intended to be resolved wiht IoC</param>
        public BankAccountRepository(IMainModuleUnitOfWork unitOfWork, ITraceManager traceManager) : base(unitOfWork, traceManager) { }


        #endregion Constructor

        #region IBankAccountRepository implementations

        public IEnumerable<BankAccount> FindBankAccounts(ISpecification<BankAccount> spec)
        {
            //validate specification
            if (spec == (ISpecification<BankAccount>)null)
            {
                throw new ArgumentNullException("specification");
            }

            IMainModuleUnitOfWork context = this.UnitOfWork as IMainModuleUnitOfWork;

            if (context != null)
            {
                //perform operation in this repository
                return context.BankAccounts.Where(spec.SatisfiedBy());
            }
            else
                throw new InvalidOperationException(string.Format(
                                                                CultureInfo.InvariantCulture,
                                                                Messages.exception_InvalidStoreContext,
                                                                this.GetType().Name));
        }

        #endregion
    }
}
